package org.jbeer.sample.controller;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;

import org.jbeer.sample.bean.User;
import org.jbeer.sample.bean.service.UserService;

import com.jbeer.framework.annotation.Action;
import com.jbeer.framework.annotation.Controller;
import com.jbeer.framework.annotation.RefBean;
import com.jbeer.framework.config.Configurate;
import com.jbeer.framework.enumeration.RequestType;
import com.jbeer.framework.exception.MessageException;
import com.jbeer.framework.exception.ScanClassException;
import com.jbeer.framework.message.MessageUtils;
import com.jbeer.framework.properties.PropertiesContext;
import com.jbeer.framework.utils.ClassPathScanFileUtil;
import com.jbeer.framework.utils.ClassUtils;
import com.jbeer.framework.web.ModelAndView;
import com.jbeer.framework.web.ModelAndView.PageModelAndView;

@Controller(urlPattern="/first")
public class FirstController {

	private User user;
	@RefBean
	private UserService userService;
	
	@Action(urlPatterns="post.htm",requestType=RequestType.POST)
	public ModelAndView post() throws MessageException{
		System.out.println("post");
		PageModelAndView mav = ModelAndView.createModelAndView();
		mav.setDataMap("user", userService.getName()+" message:"+MessageUtils.getMessage("test", new Object[]{"bieber","tom"})+" properties:"+PropertiesContext.getProperties("test"));
		mav.setView("view");
		return mav;
	}
	
	@Action(urlPatterns="index.htm",requestType=RequestType.ANY)
	public String index() throws IOException, ScanClassException{
	    Set<String> fileNames = ClassPathScanFileUtil.scanClassPathFile("com/jbeer", ".properties",false);
	    Set<Class<?>> temp = ClassUtils.scanClassesByInterfaceClass(Configurate.class);
        for(String file:fileNames){
            Properties properties = new Properties();
            properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(file));
            
            System.out.println(file);
        }
		return "index";
	}

 


	/**
	 * @param user user
	 */
	
	public void setUser(User user) {
		this.user = user;
	}
	
	
}
